<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\AppConfig;
use Common\Models\ArticleFaquan;
use Common\Models\Category;
use Common\Models\CategoryHaodanku;
use Common\Models\Collects;
use Common\Models\DouyinCategory;
use Common\Models\Footprints;
use Common\Models\Hot;
use Common\Models\JdCategory;
use Common\Models\Material;
use Library\ConstKey\RedisKey;
use Library\ConstKey\SystemKey;
use Library\Tools\RedisTools;
use Library\Validators\INArray;
use Services\Api\GoodsApiServices;
use ServicesApi\Response\GoodsResponse;

class GoodsController extends ApiController
{
	public $Collects;
	public function initialize()
	{
		parent::initialize();
	}


    //抖音分类
    public function home_douyinCategoryAction(){
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $douyinCategory = new DouyinCategory();
            $res = $douyinCategory->getdouyinCategroy();
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($res);
    }

    /**
     * 获取抖音首页分类数据
     */
    public function dhindexAction(){
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $goodsApiServices = new GoodsApiServices();
            $responseResult = $goodsApiServices->dhindex(['type'=>12]);
            if($responseResult['status']){
                $res = $responseResult['data'];
                (new RedisTools())->set($redis_key,$res);
            }else{
                $this->fail(-1,$responseResult['msg']);
            }
        }
        $this->ok($res);
    }


    /**
     * 抖音榜单
     */
    public function dyRankitemListAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }
        if(!isset($this->params['pageSize']) || empty($this->params['pageSize'])){
            $this->params['pageSize'] = 10;
        }
        if(!isset($this->params['categoryId']) || empty($this->params['categoryId'])){
            $this->params['categoryId'] = 0;
        }
        if(!isset($this->params['sortId']) || empty($this->params['sortId'])){
            $this->params['sortId'] = 1;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $goodsApiServices = new GoodsApiServices();
            $responseResult = $goodsApiServices->dyRankitemList($this->params);
            if($responseResult['status']){
                $res = $responseResult['data'];
                (new RedisTools())->set($redis_key,$res);
            }else{
                $this->fail(-1,$responseResult['msg']);
            }
        }
        $this->ok($this->money_less($res));

    }



    /**
     * 获取分类
     */
    public function categoryAction(){
        if (!INArray::platformSite($this->params['type'])) {
            $this->params['type'] = "haodanku";
        }
        if(!isset($this->params['pid']) || empty($this->params['pid'])){
            $this->params['pid'] = 0;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $goodsApiServices = new GoodsApiServices();
            $responseResult = $goodsApiServices->category($this->params['type'],$this->params['pid']);
            if(!$responseResult['status']){
                $this->fail(-1,$responseResult['msg']);
            }
            (new RedisTools())->set($redis_key,$responseResult['data']);
            $res = $responseResult['data'];
        }
        $this->ok($res);
    }


    //商品列表
    public function goods_listAction(){
        if(!isset($this->params['site']) || empty($this->params['site'])){
            $this->params['site'] = 'haodanku';
        }else{
            if (!INArray::platformSite($this->params['site'])) {
                $this->fail(-1, '站点错误');
            }
        }
        if(!isset($this->params['nav']) || empty($this->params['nav'])){
            $this->params['nav'] = 3;
        }
        if(!isset($this->params['category_id']) || empty($this->params['category_id'])){
            $this->params['category_id'] = 1;
        }
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size'] = 10;
        }
        if(!isset($this->params['order_by']) || empty($this->params['order_by'])){
            $this->params['order_by'] = 'sale';
        }
        if(!isset($this->params['sort']) || empty($this->params['sort'])){
            $params['sort'] = 'desc';
        }
        if(!isset($this->params['video']) || empty($this->params['video'])){
            $this->params['video'] = '0';
        }
        if(!isset($this->params['is_compoon']) || empty($this->params['is_compoon'])){
            $this->params['is_compoon'] = '1';
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $goodsapiServices = new GoodsApiServices();
            $responseResult = $goodsapiServices->goods_list($this->params['site'],$this->params);
            if(!$responseResult['status']){
                $this->fail(-1,$responseResult['msg']);
            }
            (new RedisTools())->set($redis_key,$responseResult['data']);
            $res = $responseResult['data'];
        }
        $res=$this->money_less($res);
        $this->ok($res);
    }

    /*
     * 商品详情
     */
    public function goods_detailAction(){
        if(!isset($this->params['site']) || empty($this->params['site'])){
            $this->params['site'] = 'haodanku';
        }else{
            if (!INArray::platformSite($this->params['site'])) {
                $this->fail(-1, '站点错误');
            }
        }
        if(!isset($this->params['goods_id']) || empty($this->params['nav'])){
            $this->params['nav'] = 3;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $goodsApiServices = new GoodsApiServices();
            $responseResult = $goodsApiServices->goods_detail($this->params['site'],$this->params['goods_id']);
            if(!$responseResult['status']){
                $this->fail(-1,$responseResult['msg']);
            }

            (new RedisTools())->set($redis_key,$responseResult['data']);
            (new Footprints())->add_footprints(['user_id'=>$this->user_info['id'],'site'=>$this->params['site'],'goods_id'=>$this->params['goods_id']]);
            $res = $responseResult['data'];
        }
        $res['IsColletc'] = (new Collects())->is_collect_user($this->user_info['id'],$this->params['goods_id'],$this->params['site']);
        $this->ok($res);
    }



    //商品搜索
    public function goods_searchAction(){
        if(!isset($this->params['site']) || empty($this->params['site'])){
            $this->params['site'] = 'haodanku';
        }else{
            if (!INArray::platformSite($this->params['site'])) {
                $this->fail(-1, '站点错误');
            }
        }

        if(!isset($this->params['keyword']) || empty($this->params['keyword'])){
            if($this->params['site'] != 'pdd') {
                $this->fail(-1, '请填写查询关键字');
            }else{
                $this->params['keyword'] = '';
            }
        }

        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size'] = 10;
        }

        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }

        if(!isset($this->params['order_by']) || empty($this->params['order_by'])){
            $this->params['order_by'] = 'sale';
        }

        if(!isset($this->params['sort']) || empty($this->params['sort'])){
            $this->params['sort'] = 'desc';
        }

        if(!isset($this->params['coupon']) || empty($this->params['coupon'])){
            $this->params['coupon'] = '0';
        }

        if(isset($this->params['range_list']) && !empty($this->params['range_list'])){
            $this->params['range_list'] = 1;
        }else{
            $this->params['range_list'] = 0;
        }
        $redis_key = Common::redisKey($this->params);
        if($this->params['site'] == 'haodanku'){
            $responseResult=$this->haodanku->goods_search($this->params);
        }
        if($this->params['site'] == 'jd'){
            $responseResult=$this->jd->goods_search($this->params);
        }
        if($this->params['site'] == 'pdd'){
            $this->params['pid']     = $this->user_info['pdd_pid'];
            $responseResult=$this->pdd->goods_search($this->params);
        }
        if($this->params['site'] == 'vph'){
            $responseResult=$this->vph->goods_search($this->params);
        }
        if ($this->params['site'] == 'suning') {
            //苏宁地址搜索不到只能截取商品名称
            if (strpos($this->params['keyword'], 'http')) {
                $this->params['keyword'] = trim(strstr($this->params['keyword'], 'http', true));
            }
            $responseResult = $this->suning->goods_search($this->params);
        }
        if($responseResult['status']){
            $this->ok($this->money_less($responseResult['data']));
        }else{
            $this->fail(-1,$responseResult['msg']);
        }
    }


    //今日值得买
    public function home_todayAction(){
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->get_today();
            (new RedisTools())->set($redis_key,$res);
        }
        $res=$this->money_less($res);
        $this->ok($res);
    }


    //抖音
    public function home_douyinAction(){
        if(!isset($this->params['category_id']) || empty($this->params['category_id'])){
            $this->params['category_id'] = 0;
        }

        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }

        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size'] = 10;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->douyin($this->params);
            if($res['msg'] != 'SUCCESS'){
                $this->fail(-200,$res['msg']);
            }
            $res=['next_page'=>$res['min_id'],'data'=>$res['data']];
            (new RedisTools())->set($redis_key,$res);
        }
        $res['data']=$this->money_less($res['data'],'haodanku');
        $this->ok($res);
    }

    /**
     *  获取折淘客的商品分类
     */
    public function CategoryHaodanke_oldAction(){
        if(!isset($this->params['cat_id']) || empty($this->params['cat_id'])){
            $this->params['cat_id'] = 0;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS) {
            $res = $this->haodanku->super_classify($this->params);
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($res);
    }

    /**
     *  获取折淘客的商品分类
     */
    public function CategoryHaodankeAction(){
        if(!isset($this->params['cat_id']) || empty($this->params['cat_id'])){
            $this->params['cat_id'] = 0;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS) {
            $categoryhaodanku = new CategoryHaodanku();
            $res = $categoryhaodanku->get_find($this->params['cat_id']);
            foreach ($res as $key => $value) {
                unset($res[$key]['id']);
                $res[$key]['category_id'] = (int)$value['category_id'];
                if (isset($value['child'])) {
                    foreach ($value['child'] as $key1 => $value1) {
                        $res[$key]['child'][$key1]['category_id'] = (int)$value1['category_id'];
                    }
                }
            }
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($res);
    }



    /**
     *  首页获取商品分类 获取折淘客的商品分类
     */
    public function CategoryHaodanke_indexAction(){
        if(!isset($this->params['cat_id']) || empty($this->params['cat_id'])){
            $this->params['cat_id'] = 0;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS) {
            $categoryhaodanku = new CategoryHaodanku();
            $res = $categoryhaodanku->get_find_index($this->params['cat_id']);
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($res);
    }




    /**
     * 商品筛选API 聚划算
     */
    public function Home_free_shipAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }
        if(!isset($this->params['cid']) || empty($this->params['cid'])){
            $this->params['cid'] = 0;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size'] = 10;
        }
        if(!isset($this->params['type']) || empty($this->params['type'])){
            $this->params['type'] = 2;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->zhetaoke->api_all_juhuasuan($this->params);
            if($res['code'] == 0){
                $this->fail(-201,'数据获取完毕');
            }
            $res=['next_page'=>$res['min_id'],'data'=>$res['data']];
            (new RedisTools())->set($redis_key,$res);
        }
        $res['data']=$this->money_less($res['data'],'haodanku');
        $this->ok($res);
    }


    //高佣金
    public function Home_high_commissionAction(){
        if(!isset($this->params['min_id']) || empty($this->params['min_id'])){
            $this->params['min_id'] = 1;
        }
        if(!isset($this->params['cid']) || empty($this->params['cid'])){
            $this->params['cid'] = 0;
        }
        if(!isset($this->params['back']) || empty($this->params['back'])){
            $this->params['back'] = 10;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->high_commission($this->params);
            if($res['msg'] != 'SUCCESS'){
                $this->fail(-200,$res['msg']);
            }
            $res=['next_page'=>$res['min_id'],'data'=>$res['data']];
            (new RedisTools())->set($redis_key,$res);
        }
        $res['data']=$this->money_less($res['data'],'haodanku');
        $this->ok($res);
    }

    //首页热门
    public function Home_hotAction(){
        if(!isset($this->params['min_id']) || empty($this->params['min_id'])){
            $this->params['min_id'] = 1;
        }
        if(!isset($this->params['cid']) || empty($this->params['cid'])){
            $this->params['cid'] = 0;
        }
        if(!isset($this->params['back']) || empty($this->params['back'])){
            $this->params['back'] = 10;
        }
        if(!isset($this->params['sale_type']) || empty($this->params['sale_type'])){
            $this->params['sale_type'] = 1;
        }else{
            if($this->params['sale_type'] != 1 && $this->params['sale_type'] != 2 && $this->params['sale_type'] != 3 && $this->params['sale_type'] != 4){
                $this->fail(-1,'榜单类型错误');
            }
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->hot($this->params);
            if($res['msg'] != 'SUCCESS'){
                $this->fail(-201,$res['msg']);
            }

            $res=['next_page'=>$res['min_id'],'data'=>$res['data']];
            (new RedisTools())->set($redis_key,$res);
        }
        $res['data']=$this->money_less($res['data'],'haodanku');
        $this->ok($res);
    }


    //首页热搜关键字
    public function Home_hot_keyAction(){
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->hot_key($this->params);
            (new RedisTools())->set($redis_key,$res);
        }
        $count_key = count($res);
        if($count_key>30){
            $keycount=30;
        }else{
            $keycount=$count_key;
        }
        $new_keyres = array_rand($res,$keycount);
        $data=[];
        foreach($new_keyres as $key=>$value ){
            $data[$key]['keyword'] = $res[$value]['keyword'];
        }
        $this->ok($data);
    }



    /**
     * 获取京东 京粉精选商品查询接口
     */
    public function jd_querygoodsAction(){
        if(!isset($this->params['site']) || empty($this->params['site'])){
            $this->params['site'] = 'jd';
        }else{
            if(!in_array($this->params['site'],['haodanku','jd','pdd','suning','vph'])){
                $this->fail(-1,'站点错误');
            }
        }
        if(!isset($this->params['pageSize']) || empty($this->params['pageSize'])){
            $this->params['pageSize'] = 10;
        }
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }

        if($this->params['site'] == 'jd') {
            if (!isset($this->params['eliteId']) || empty($this->params['eliteId'])) {
                $this->params['eliteId'] = 22;
            }
            if (!isset($this->params['order_by']) || empty($this->params['order_by'])) {
                $this->params['order_by'] = 'inOrderCount30DaysSku';
            }
            if (!isset($this->params['sort']) || empty($this->params['sort'])) {
                $this->params['sort'] = 'asc';
            }
            $redis_key = Common::redisKey($this->params);
            $res = (new RedisTools())->get($redis_key);
            if(!$res && !SystemKey::ISREDIS){
                $res = $this->jd->jd_querygoods($this->params);
                (new RedisTools())->set($redis_key,$res);
            }
            $res=$this->money_less($res,'jd');
            $this->ok($res);
        }elseif ($this->params['site'] == 'pdd'){           //后面添加，前端需要
            if(!isset($this->params['type']) || empty($this->params['type'])){
                $this->params['type']=0;
            }
            $this->params['page_size']=$this->params['pageSize'];
            $redis_key = Common::redisKey($this->params);
            $res = (new RedisTools())->get($redis_key);
            if(!$res && !SystemKey::ISREDIS){
                $res=$this->pdd->recommendget($this->params);
                (new RedisTools())->set($redis_key,$res);
            }
            $res=$this->money_less($res,'pdd');
            $this->ok($res);
        }
    }


    //猜你喜欢
    public function you_also_likeAction(){
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->zhetaoke->you_also_like($this->params);
            (new RedisTools())->set($redis_key,$res);
        }
        $res=$this->money_less($res);
        $this->ok($res);
    }



    //品牌列表
    public function brand_listAction(){
        $params=$this->params;
        if(!isset($params['page_size']) || empty($params['page_size'])){
            $params['page_size']=5;
        }
        if(!isset($params['page']) || empty($params['page'])){
            $params['page']=1;
        }else{
            $params['page'] = $params['page'];
        }
        if(!isset($params['category_id']) || empty($params['category_id'])){
            $params['category_id']=0;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->brand_list($params);
            foreach($res['data'] as $k =>$v){
                $res['data'][$k]['items']=$this->money_less($v['items'],'haodanku');
            }
            $res=['next_page'=>$res['min_id'],'datas'=>$res['data']];
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($res);
    }


    //品牌类型
    public function brand_typeAction(){
        $redis_key="brand_type";
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS) {
            $res = [
                ["category_id" => 0, "name" => "精选"],
                ["category_id" => 1, "name" => "母婴童品"],
                ["category_id" => 2, "name" => "百变女装"],
                ["category_id" => 3, "name" => "食品酒水"],
                ["category_id" => 4, "name" => "居家日用"],
                ["category_id" => 5, "name" => "美妆洗护"],
                ["category_id" => 6, "name" => "品质男装"],
                ["category_id" => 7, "name" => "舒适内衣"],
                ["category_id" => 8, "name" => "箱包配饰"],
                ["category_id" => 9, "name" => "男女鞋靴"],
                ["category_id" => 10, "name" => "宠物用品"],
                ["category_id" => 11, "name" => "数码家电"],
                ["category_id" => 12, "name" => "车品文体"],
            ];
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($res);
    }



    //品牌详情
    public function brand_detailAction(){
        $params=$this->params;
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->brand_detail($this->params);
            (new RedisTools())->set($redis_key,$res);
        }
        $res['items']=$this->money_less($res['items']);
        $this->ok($res);
    }

    public function faquanAction(){
        $params=$this->params;
        if(!isset($params['page']) || empty($params['page'])){
            $params['page']=1;
        }

        if(!isset($params['page_size']) || empty($params['page_size'])){
            $params['page_size']=10;
        }

        if(!isset($params['cid']) || empty($params['cid'])){
            $params['cid']=0;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if($params['cid'] == 0) {           //全部的时候调用好单库的发圈接口
            if (!$res && !SystemKey::ISREDIS) {
                $res = $this->haodanku->faquan_new($params['page']);
                (new RedisTools())->set($redis_key,$res);
            }
        }else{                      //查找自己的商品库
            if(!$res && !SystemKey::ISREDIS) {
                $articleFaquan = new ArticleFaquan();
                $ku_res = $articleFaquan->find_list_new($this->params['cid'],$params);
                $res=[];
                foreach($ku_res as $key=>$value){
                    $res[$key]['itemid'] = isset($value['itemid']) ? $value['itemid'] : '';
                    $res[$key]['title'] = isset($value['title']) ? $value['title'] : '';
                    $res[$key]['dummy_click_statistics'] = isset($value['dummy_click_statistics']) ? $value['dummy_click_statistics'] : rand(500,10000);
                    $res[$key]['sola_image'] = isset($value['itempic']) ? explode(',',$value['itempic'])[0] : [];
                    $res[$key]['itempic'] = isset($value['itempic']) ? explode(',',$value['itempic']) : [];
                    $res[$key]['copy_content'] = isset($value['content']) ? base64_decode($value['content']) : '';
                    $res[$key]['copy_comment'] = '復·制该条评论👉🏻  (复制后自动生成口令)咑·開【📱🍑 tao寳】即可';

                }
                (new RedisTools())->set($redis_key,$res);
            }
        }
        $this->ok($res);
    }

    /**
     * 解析商品编号
     */
    public function openshangpinAction(){
        if(!isset($this->params['title']) || empty($this->params['title'])){
            $this->fail(-1, '没有复制任何东西');
        }
        $GoodsApiServices = new GoodsApiServices();
        $response = $GoodsApiServices->ai_search($this->user_info,$this->params['title']);
        if($response['status']){
            $this->ok($response['data']);
        }
        $this->fail(-1,$this->params['title'],urldecode($this->params['title']));
    }


    //pdd运营频道商品查询API
    public function recommendgetAction(){
        $params=$this->params;
        if(!isset($params['page_size']) || empty($params['page_size'])){
            $params['page_size']=10;
        }
        if(!isset($params['page']) || empty($params['page'])){
            $params['page']=0;
        }else{
            $params['page'] = $params['page']+$params['page_size'];
        }
        if(!isset($params['type']) || empty($params['type'])){
            $params['type']=0;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->pdd->recommendget($params);
            (new RedisTools())->set($redis_key,$res);
        }
        $res=$this->money_less($res,'pdd');
        $this->ok($res);
    }


    /**
     * 大淘客 商品列表
     */
    public function getgoodslistAction(){
        $params=$this->params;
        if(!isset($params['page_size']) || empty($params['page_size'])){
            $params['page_size']=10;
        }
        if(!isset($params['page']) || empty($params['page'])){
            $params['page']=1;
        }
        if(!isset($params['order_by']) || empty($params['order_by'])){
            $params['order_by']='moren';
        }
        if(!isset($params['sort']) || empty($params['sort'])){
            $params['sort']='desc';
        }

//        if(!isset($params['cid']) || empty($params['cid'])){
//            $params['cid']=1;
//        }

        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res = $this->taoke->getgoodslist($params);
            if($res['code']){
                $res=$res['data'];
                (new RedisTools())->set($redis_key,$res);
            }else{
                $this->fail(-200,'暂无数据');
            }
        }
        $res = $this->money_less($res);
        $this->ok($res);
    }



    /**
     * 海淘商品API（天猫国际API）
     */
    public function haitaoAction(){
        $params=$this->params;
        if(!isset($params['page_size']) || empty($params['page_size'])){
            $params['page_size']=10;
        }
        if(!isset($params['page']) || empty($params['page'])){
            $params['page']=1;
        }
        if(!isset($params['order_by']) || empty($params['order_by'])){
            $params['order_by']='new';
        }
        if(!isset($params['sort']) || empty($params['sort'])){
            $params['sort']='desc';
        }
        if(!isset($params['cid']) || empty($params['cid'])){
            $params['cid']=0;
        }

        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res = $this->zhetaoke->haitao($params);
            if($res['code']){
                $res=$res['data'];
                (new RedisTools())->set($redis_key,$res);
            }else{
                $this->fail(-1,'暂无数据');
            }
        }
        $res = $this->money_less($res);
        $this->ok($res);
    }




    //淘折客天猫超市商品
    public function tanmao_supermaketAction(){
        $params=$this->params;
        if(!isset($params['page_size']) || empty($params['page_size'])){
            $params['page_size']=10;
        }
        if(!isset($params['page']) || empty($params['page'])){
            $params['page']=1;
        }
        if(!isset($params['cid']) || empty($params['cid'])){
            $params['cid']=0;
        }

        if(!isset($params['order_by']) || empty($params['order_by'])){
            $params['order_by']='new';
        }

        if(!isset($params['sort']) || empty($params['sort'])){
            $params['sort']='desc';
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res = $this->zhetaoke->tanmao_supermaket($params);
            if($res['code']){
                $res=$res['data'];
                (new RedisTools())->set($redis_key,$res);
            }else{
                $this->fail(-200,'暂无数据');
            }
        }
        $res = $this->money_less($res);
        $this->ok($res);
    }


    public function goods_posterAction(){
        if(!isset($this->params['site']) || empty($this->params['site'])){
            $this->params['site']='haodanku';
            //   $this->fail(-1,'站点不能为空');
        }
        if(!isset($this->params['url']) || empty($this->params['url'])){
            $this->fail(-1,'链接不能为空');
        }
        if(!isset($this->params['goods_id']) || empty($this->params['goods_id'])){
            $this->fail(-1,'商品id不能为空');
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            if($this->params['site'] == 'haodanku' ||$this->params['site'] == 'taobao'){
                $this->params['site']='haodanku';
                $res=$this->haodanku->goods_detail($this->params['goods_id']);
            }
            if($this->params['site'] == 'jd'){
                $res=$this->jd->goods_detail($this->params['goods_id']);
            }
            if($this->params['site'] == 'pdd'){
                $res=$this->pdd->goods_detail($this->params['goods_id']);
            }
            (new RedisTools())->set($redis_key,$res);
        }
        $res=$this->money_less([$res],$this->params['site'])[0];
        if($this->params['site'] == 'haodanku' ||$this->params['site'] == 'taobao'){
            $res['turn_url'] = "复制这条復製这条，({$this->params['url']})，进入【Tao宝】即可抢购";
        }
        if($this->params['site'] == 'jd'){
            $res['turn_url'] = "复制这条復製这条，({$this->params['url']})，进入京东即可抢购";
        }
        if($this->params['site'] == 'pdd'){
            $res['turn_url'] = "复制这条復製这条，({$this->params['url']})，进入拼多多即可抢购";
        }
        $base64_img = Common::goods_poster($res);
        $this->ok($base64_img);
    }

    /**
     * 大淘客联想词
     */
    public function searchSuggestionAction(){
        if(!isset($this->params['keyWords']) || empty($this->params['keyWords'])){
            $this->fail(-1,'关键字不能为空');
        }
        if(!isset($this->params['type']) || empty($this->params['type'])){
            $this->params['type'] = 1;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->taoke->searchsuggestion($this->params);
            (new RedisTools())->set($redis_key,$res);
        }
        if(empty($res)){
            $this->ok([], -200, '列表没有消息');
        }else{
            $this->ok($res);
        }
    }


    /**
     * 折淘客 快抢商品API
     */
    public function fastbuyAction(){
        if(!isset($this->params['hour_type']) || empty($this->params['hour_type'])){
            $this->params['hour_type']=7;
        }
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page']=1;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->fastbuy($this->params);
            (new RedisTools())->set($redis_key,$res);
        }
        $result['data'] = $this->money_less($res);
        $this->ok($result);
    }


    /**
     * 平台站点信息
     */
    public function platform_siteAction(){
        $data=[
            ['site'=>'haodanku','name'=>'淘宝'],
            ['site'=>'jd','name'=>'京东'],
            ['site'=>'pdd','name'=>'拼多多'],
            ['site'=>'vph','name'=>'唯品会'],
            ['site'=>'suning','name'=>'苏宁'],
            ['site'=>'douyin','name'=>'抖音'],
        ];
        $this->ok($data);
    }



    /**
     * 搜索 推荐搜索
     */
    public function get_search_tagAction(){
        //热点
        $hot      = new Hot();
        $hot_id2  = $hot->find_by_id(1);
        $material = new Material();
        $data     = $material->getListByIds($hot_id2['ids'], 1, $this->user_info['id']);
        $this->ok($data);
    }


    /**
     * 热卖
     */
    public function hot_saleAction()
    {
        //热点
        $hot      = new Hot();
        $hot_id2  = $hot->find_by_id(2);
        $material = new Material();
        $data     = $material->getListByIds($hot_id2['ids'], 1, $this->user_info['id']);
        $this->ok($data);
    }


    /**
     * 超级返菜单
     */
    public function super_menuAction(){
        $data = '{"name":"超级返","column_id":"5","children":[{"column_second_id":"13366","name":"精选"},{"column_second_id":"13372","name":"男装"},{"column_second_id":"13373","name":"内衣"},{"column_second_id":"13374","name":"母婴"},{"column_second_id":"13375","name":"食品"},{"column_second_id":"13368","name":"家居家装"},{"column_second_id":"13369","name":"数码家电"},{"column_second_id":"13370","name":"鞋包配饰"},{"column_second_id":"13371","name":"美妆个护"},{"column_second_id":"13376","name":"运动户外"}]}';
        $data_result = json_decode($data,true);
        $this->ok($data_result['children']);
    }

    /**
     * 超级返
     */
    public function super_fanAction(){
        if(!isset($this->params['column_second_id']) || empty($this->params['column_second_id'])){
            $this->params['column_second_id']='13366';
        }
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page']=1;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size']=10;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res = $this->taobao->TbkDgOptimusMaterialRequest($this->params['column_second_id'],$this->params['page'],$this->params['page_size']);
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($res);
    }


    /**
     * 母婴专区分类
     */
    public function mother_baby_cateAction(){
        $data=[
            ['id'=>4040,'name'=>'备孕'],
            ['id'=>4041,'name'=>'0-6月'],
            ['id'=>4042,'name'=>'7-12月'],
            ['id'=>4043,'name'=>'1-3岁'],
            ['id'=>4044,'name'=>'4-6岁'],
            ['id'=>4045,'name'=>'7-12岁'],
        ];
        $this->ok($data);
    }



    /**
     *母婴专区
     */
    public function mother_babyAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page']=1;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size']=10;
        }
        if(!isset($this->params['type']) || empty($this->params['type'])){
            $this->params['type']=4040;
        }else{
            if(!in_array($this->params['type'], [4040,4041,4042,4043,4044,4045])){
                $this->fail(-1,'type错误');
            }
        }
        /**
         * @link https://market.m.taobao.com/app/qn/toutiao-new/index-pc.html?spm=a219t.11816995.1998910419.d1856e3ff.675c75a5BYYRPd#/detail/10628875?_k=vt9nz0
         * @see taobao::TbkDgOptimusMaterialRequest
         */
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS) {
            $data = $this->taobao->TbkDgOptimusMaterialRequest($this->params['type'], $this->params['page'], $this->params['page_size']);
            if (!empty($data)) {
                $this->params['page'] += 1;
            }
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($data);
    }


    public function category_jttAction(){
        $data=[
            ['category_id'=>1,'name'=>'居家日用'],
            ['category_id'=>2,'name'=>'食品'],
            ['category_id'=>3,'name'=>'生鲜'],
            ['category_id'=>4,'name'=>'图书'],
            ['category_id'=>5,'name'=>'美妆个护'],
            ['category_id'=>6,'name'=>'母婴'],
            ['category_id'=>7,'name'=>'数码家电'],
            ['category_id'=>8,'name'=>'内衣'],
            ['category_id'=>9,'name'=>'配饰'],
            ['category_id'=>10,'name'=>'女装'],
            ['category_id'=>11,'name'=>'男装'],
            ['category_id'=>12,'name'=>'鞋品'],
            ['category_id'=>13,'name'=>'家装家纺'],
            ['category_id'=>14,'name'=>'文娱车品'],
            ['category_id'=>15,'name'=>'箱包'],
            ['category_id'=>16,'name'=>'户外运动']
        ];
        $this->ok($data);
    }



    //大淘客快抢商品
    public function fast_dataoke_buyAction(){
        if(isset($this->params['page']) && !empty($this->params['page']) && $this->params['page'] == 2){
            $data['data']=[];
            $this->ok($data);
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS) {
            switch ($this->params['type']) {
                case 8:
                    $date = date('Y-m-d') . ' 08:00:00';
                    break;
                case 10:
                    $date = date('Y-m-d') . ' 10:00:00';
                    break;
                case 15:
                    $date = date('Y-m-d') . ' 15:00:00';
                    break;
                case 17:
                    $date = date('Y-m-d') . ' 17:00:00';
                    break;
                case 22:
                    $date = date('Y-m-d') . ' 22:00:00';
                    break;
                default:
                    $date = date('Y-m-d') . ' 08:00:00';
                    break;
            }
            $res = $this->taoke->ddq_goods_list($date);
            (new RedisTools())->set($redis_key,$res);
        }
        $res['data'] = $this->money_less($res);
        $this->ok($res);
    }


    /**
     * 大淘客9.9包邮
     */
    public function opgoodslistAction(){
        $params=$this->params;
        if(!isset($params['page_size']) || empty($params['page_size'])){
            $params['page_size']=10;
        }
        if(!isset($params['page']) || empty($params['page'])){
            $params['page']=1;
        }else{
            $params['page'] = $params['page']+$params['page_size'];
        }
        if(!isset($params['cid']) || empty($params['cid'])){
            $params['cid']=-1;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res = $this->taoke->opgoodslist($params);
            if($res['code']){
                $res=$res['data'];
                (new RedisTools())->set($redis_key,$res);
            }else{
                $this->fail(-200,'暂无数据');
            }
        }
        $res = $this->money_less($res);
        $this->ok($res);
    }



    public function jdnine_to_nineAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page']=1;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size']=10;
        }
        if(!isset($this->params['cid']) || empty($this->params['cid'])){
            $this->params['goods_new_type']=1;
        }else{
            $this->params['goods_new_type']=$this->params['cid'];
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $result = $this->jtt->nine_ti_nine($this->params);
            if($result['code']){
                $res = $result['data'];
                (new RedisTools())->set($redis_key,$res);
            }else{
                $this->fail(-1,$result['msg']);
            }
        }
        $this->ok($res);
    }


    public function home_index_goodsAction()
    {
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        if (!isset($this->params['page_size']) || empty($this->params['page_size'])) {
            $this->params['page_size'] = 10;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if (!$res && !SystemKey::ISREDIS) {
            $res = $this->taoke->explosive_goods_list($this->params);
            (new RedisTools())->set($redis_key,$res);
        }
        $res = $this->money_less($res);
        $this->ok($res);
    }


    //京推推精选好货
    public function jd_jxhhAction(){
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        if (!isset($this->params['page_size']) || empty($this->params['page_size'])) {
            $this->params['page_size'] = 10;
        }
        if (!isset($this->params['order_by']) || empty($this->params['order_by'])) {
            $this->params['order_by'] = 'goodComments';
        }
        if (!isset($this->params['sort']) || empty($this->params['sort'])) {
            $this->params['sort'] = 'desc';
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res = $this->jtt->jxhh($this->params);
            if(!$res['code']){
                $this->ok([]);
            }
            (new RedisTools())->set($redis_key,$res);
            $res = $res['data'];
        }
        $res = $this->money_less($res);
        $this->ok($res);
    }


    /*
     相关推荐
     */
    public function guess_you_likeAction(){
        $params=$this->params;
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            $res=$this->haodanku->related_suggest($params['goods_id']);
            (new RedisTools())->set($redis_key,$res);
        }
        $res=$this->money_less($res);
        $this->ok($res);
    }




    /**
     * 智能转链
     */
    public function intelligence_turnAction()
    {
        if (!isset($this->params['source']) || empty($this->params['source'])) {
            $this->fail(-1, 'source');
        }
        if (!isset($this->params['title']) || empty($this->params['title'])) {
            $this->fail(-1, '没有复制任何东西');
        }
        if (!isset($this->params['user_id']) || empty($this->params['user_id'])) {
            $this->fail(-1, '缺少user_id');
        }
        $redis_key = Common::redisKey($this->params);
        $data = (new RedisTools())->get($redis_key);
        if (!$data && !SystemKey::ISREDIS) {
            $data = [];
            $urls = explode(',', $this->params['title']);
            foreach ($urls as $row) {
                $parData['msg'] = '';
                $parData['status'] = false;
                $parData['data'] = (object)null;
                $rowUrl = urldecode($row);
                $tbRes = $this->zhetaoke->open_shangpin($rowUrl);
                if ($tbRes['code'] == 1) {
                    if (!isset($this->params['tb_rid']) || empty($this->params['tb_rid']) || !isset($this->params['tb_sid']) || empty($this->params['tb_sid'])) {
                        $parData['msg'] = '请先完成淘宝授权';

                    } else {
                        $klRes = $this->haodanku->taobao_turn($this->params['tb_rid'], $tbRes['msg'], $this->params['source'], $this->params['tb_sid']);      //商品转链

                        if ($klRes['status'] == true) {
                            $klParams['url'] = $klRes['msg'];
                            $klParams['title'] = $rowUrl;
                            $klParams['image'] = "";
                            $tbKl = $this->taobao->couponUrlToText($klParams);
                            if ($tbKl['code'] == 1) {
                                $result['site'] = "haodanku";
                                $result['object'] = $tbKl['model'];
                            }
                        }
                    }
                }

                preg_match('/(http|https|ftp)(.)*([a-z0-9\-\.\_])+/i', $rowUrl, $array2);
                if (!empty($array2['0'])) {
                    $title = $array2[0];
                    //pdd
                    if (strpos($title, 'pinduoduo.com') !== false) {//如果是拼多多的短连接
                        if (!$this->params['pdd_pid']) {
                            $parData['msg'] = '请先完成拼多多授权';
                        } else {

                            $res = get_headers($array2[0], true);

                            $title = urldecode($res['Location']);
                            $res = $this->pdd->unitUrlGen($title, $this->params['user_id'], $this->params['pdd_pid']);
                            if ($res['status']) {
                                $result['site'] = "pdd";
                                $result['object'] = $res['short_url'];
                            }
                        }
                    }

                    if (strpos($title, 'yangkeduo.com') !== false) {//拼多多活动转链 只能转活动后台的
                        //https://jinbao.pinduoduo.com/promotion/activity-sync-detail?id=784
                        if (!$this->params['pdd_pid']) {
                            $parData['msg'] = '请先完成拼多多授权';
                        } else {
                            $res = $this->pdd->unitUrlGen($title, $this->params['user_id'], $this->params['pdd_pid']);
                            if ($res['status']) {
                                $result['site'] = "pdd";
                                $result['object'] = $res['short_url'];
                            }
                        }
                    }

                    //京东
                    if (strpos($title, 'jd.com') !== false) {
                        $res = $this->jd->intellectCreateturn($this->params['user_id'], $title);
                        if ($res['status']) {
                            $result['site'] = "jd";
                            $result['object'] = $res['short_url'];
                        }
                    }

                    //苏宁
                    if (strpos($title, 'suning') !== false) {
                        $res = get_headers($title, true);
                        if (isset($res['Location'])) {
                            $title = urldecode($res['Location']);
                        }
                        if (!$this->params['su_pid']) {
                            $parData['msg'] = '请先完成苏宁授权';
                        } else {
                            $suRes = $this->suning->urlQueryRequest($this->params['su_pid'], $title);
                            if ($suRes['status']) {
                                $result['site'] = "suning";
                                $result['object'] = $suRes['short_url'];
                            }
                        }
                    }

                    //唯品会
                    if (strpos($title, 'vip.com') !== false) {

                        $res = $this->vph->vipLinkCheck($title);
                        if ($res['status']) {
                            $resUrl = $this->vph->genByVIPUrl($res['data']);
                            $result = [];
                            if ($resUrl['status']) {
                                foreach ($resUrl['data'] as $aval) {
                                    $temp['site'] = "vph";
                                    $temp['object'] = $aval;
                                    $result = $temp;
                                }
                            }
                        }
                    }

                }

                if (!empty($result)) {
                    $parData['status'] = true;
                    $parData['data'] = $result;
                    if (!empty($parData['msg'])) {
                        $parData['status'] = false;
                        $parData['data'] = (object)null;;
                    }
                }
                $data[] = $parData;
            }
            (new RedisTools())->set($redis_key,$data);
        }
        $this->ok($data);
    }



    /**
     * 猜你喜欢
     * 相关推荐
     */
    public function related_suggestAction()
    {
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        if (!isset($this->params['page_size']) || empty($this->params['page_size'])) {
            $this->params['page_size'] = 10;
        }
        if (!isset($this->params['goods_id']) || empty($this->params['goods_id'])) {
            $this->params['goods_id'] = '';
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if (!$res && !SystemKey::ISREDIS) {
            $res = $this->haodanku->related_suggest($this->params['goods_id']);
            (new RedisTools())->set($redis_key,$res);
        }
        $res = $this->money_less($res);
        $this->ok($res);
    }


    /**
     * 各大平台商品
     */
    public function bangdanAction(){
        if(!isset($this->params['site']) || empty($this->params['site'])){
            $this->fail(-1,'站点不能为空');
        }
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }

        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size'] = 10;
        }
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS){
            switch($this->params['site']){
                case "jd":
                    $result = $this->jtt->jxhh($this->params);
                    $res = $result['code'] ? $result['data'] : [];
                    break;
                case "pdd":
                    $res = $this->pdd->goods_list($this->params);
                    break;
                case "suning":

                    break;
                case "vph":

                    break;
            }
            (new RedisTools())->set($redis_key,$res);
        }
        $this->ok($res);
    }
}
